package org.stone.project.tx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.stone.project.tx.handle.MsgTypeHandler;
import org.stone.project.tx.service.MsgTypeHandlerAdapter;
import org.stone.project.tx.sysEnum.MsgType;
import org.stone.project.tx.sysEnum.TxType;
import org.stone.project.tx.utils.ResultUtil;

import java.util.Map;

@Service
public class MsgTypeHandlerAdapterImpl implements MsgTypeHandlerAdapter {
    @Autowired
    private Map<TxType, MsgTypeHandler> typeMsgHandlerMap;

    public void handleMsgType(String token, Integer type, JSONObject data, ChannelHandlerContext ctx) {
        String errMsg;
        if (ObjectUtil.isNotNull(type)){
            TxType sendType = TxType.getInstance(type);
            if (ObjectUtil.isNotNull(sendType)){
                MsgTypeHandler msgTypeHandler = typeMsgHandlerMap.get(sendType);
                if (ObjectUtil.isNotNull(msgTypeHandler)){
                    /**
                     * 消息处理
                     */
                    msgTypeHandler.postMsg(token,data,ctx);
                    return;


                }else {
                    errMsg = "type处理器未定义";
                }
            }else {
                errMsg = "type未定义";
            }
        }
        errMsg = "type为必传参数";
        ctx.writeAndFlush(ResultUtil.fail(MsgType.LOGIN,errMsg));
    }
}
